import os
from openpyxl import load_workbook
from openpyxl.workbook.workbook import Workbook
from openpyxl.worksheet.worksheet import Worksheet
from openpyxl.cell.cell import Cell


def worksheetReader(inWorksheet: Worksheet):
    cellA: Cell = inWorksheet["A1"]  # 获取单元格对象
    valueA = cellA.value  # 获取单元格值
    print(valueA)


def workbookToWorksheet(inWorkbook: Workbook):
    worksheet = inWorkbook.active  # 获取当前活动工作表
    if worksheet is None:  # 如果当前工作表不存在
        raise ValueError("工作表不存在")  # 抛出异常
    worksheetReader(worksheet)  # 处理工作表


def pathToWorkbook(inFilePath: str):
    if not inFilePath.endswith(".xlsx"):  # 判断文件是否为xlsx格式
        print("文件格式错误：", inFilePath)
        return
    workbook = load_workbook(inFilePath)  # 打开工作簿
    try:  # 异常处理
        workbookToWorksheet(workbook)
    finally:
        workbook.close()


def dirToPathList(inDirPath: str):
    files = os.listdir(inDirPath)
    for file in files:
        pathToWorkbook(file)


def __main__():
    dirToPathList("测试文件夹")
